﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Workbench.Operations;
using Workbench.Presentation;

namespace WorkbenchTests
{
    [TestClass]
    public class LemmatizerViewModelTests
    {
        [TestInitialize]
        public void Setup()
        {
            TestUtility.CreateAndInitializeTempDatabase();
        }

        [TestMethod]
        public void LemmatizerViewModelTests_fills_availablefiles_correctly()
        {
            var rootDirectory = ProjectOperations.GetRootDirectory();
            string fileName1 = TestUtility.CreateTempFileWithText("Content of file1", "File1.txt");
            string fileName2 = TestUtility.CreateTempFileWithText("Content of file2", "File2.txt");
            ProjectOperations.AddExistingFile(fileName1, rootDirectory.RowId);
            ProjectOperations.AddExistingFile(fileName2, rootDirectory.RowId);

            var vm = new LemmatizerViewModel();
            rootDirectory = ProjectOperations.GetRootDirectory();
            vm.FileSelectViewModel.ShowFiles(rootDirectory);

            Assert.AreEqual(2, vm.FileSelectViewModel.AvailableFiles.Count);
            Assert.AreEqual("File1.txt", vm.FileSelectViewModel.AvailableFiles[0].Name);
            Assert.AreEqual("File2.txt", vm.FileSelectViewModel.AvailableFiles[1].Name);
        }

        [TestMethod]
        public async void LemmatizerViewModelTests_fills_availablewords_correctly()
        {
            var file = TestUtility.CreateProcessedFile("Content of file1");
            var rootDirectory = ProjectOperations.GetRootDirectory();

            var vm = new LemmatizerViewModel();
            vm.FileSelectViewModel.ShowFiles(rootDirectory);
            Assert.AreEqual(1, vm.FileSelectViewModel.AvailableFiles.Count);

            await vm.LoadFile(new ProjectFileViewModel(file));
            Assert.AreEqual(3, vm.WordsListViewModel.AvailableWords.Count);
            Assert.AreEqual(3, vm.WordsListViewModel.CurrentWords.Count);
        }

        [TestMethod]
        public async void LemmatizerViewModelTests_SetBaseFormCommand_updates_selected_words()
        {
            var file = TestUtility.CreateProcessedFile("this this this.");
            var rootDirectory = ProjectOperations.GetRootDirectory();

            var vm = new LemmatizerViewModel();
            vm.FileSelectViewModel.ShowFiles(rootDirectory);
            Assert.AreEqual(1, vm.FileSelectViewModel.AvailableFiles.Count);

            await vm.LoadFile(new ProjectFileViewModel(file));
            Assert.AreEqual(3, vm.WordsListViewModel.AvailableWords.Count);
            Assert.AreEqual(3, vm.WordsListViewModel.CurrentWords.Count);

            foreach (var wordViewModel in vm.WordsListViewModel.CurrentWords)
            {
                wordViewModel.IsSelected = true;
            }
            LemmatizerOperations.AddBaseForm("this");
            vm.CurrentBaseFormContent = "this";
            Assert.AreEqual(1, vm.CurrentBaseForms.Count);
            vm.CurrentBaseForms[0].IsSelected = true;

            vm.AddBaseFormCommand.Execute(null);

            Assert.IsTrue(vm.SetBaseFormCommand.CanExecute(null));
            vm.SetBaseFormCommand.Execute(null);

            var words = vm.WordsListViewModel.CurrentWords.Select(x => x.DataModel).ToList();
            words.ForEach(x => Assert.AreEqual("this", x.BaseForm.Content));
            words.ForEach(x => Assert.AreEqual(1, x.BaseForm.RowId));
        }
    }
}
